/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objects;

import editor.WorldQ;
import graphic.Graphic;
import graphic.LoadingFrame;
import graphic.Pointer;
import input.ActionState;
import input.Input;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import klient.Bridge;
import klient.Klient;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Vector3f;

/**
 *
 * @author bwplo_000
 */
public class Quarter {
    public static int MP = 0;
    private int player; //0 jest klawiatura
    private int part;
    private Base base;
    private WorldQ myWQ;
    private Quarter nextQ;
   
    private ActionState aState;
    private int aTower = 0;
    public ActionEngine Ae;  
    public Quarter(int part, int player){
        //ładuje dane, albo inicjuje tym konstruktorem (demo)
        this.part = part;
        this.player = player;  
    }
    public void loadDemoWQ(){
         this.myWQ = new WorldQ();
        myWQ.checkPoint.add(new Vector3f(0.0f,0.0f,50.0f));
        myWQ.checkPoint.add(new Vector3f(5.0f,0.0f,50.0f));
        myWQ.checkPoint.add(new Vector3f(5.0f,0.0f,10.0f));
        myWQ.checkPoint.add(new Vector3f(10.0f,0.0f,10.0f));
        myWQ.checkPoint.add(new Vector3f(10.0f,0.0f,50.0f));
        myWQ.checkPoint.add(new Vector3f(20.0f,0.0f,50.0f));
        myWQ.checkPoint.add(new Vector3f(20.0f,0.0f,10.0f));
        myWQ.checkPoint.add(new Vector3f(30.0f,0.0f,10.0f));
        myWQ.checkPoint.add(new Vector3f(30.0f,0.0f,50.0f));
        myWQ.checkPoint.add(new Vector3f(40.0f,0.0f,50.0f));
        myWQ.checkPoint.add(new Vector3f(40.0f,0.0f,2.0f));
    }
    public void loadWQ(String name){
        try
        {
            FileInputStream fin =
                new FileInputStream(name);
            ObjectInputStream out =
                new ObjectInputStream(fin);
            this.myWQ=(WorldQ)out.readObject();
            out.close();
            fin.close();
        }catch(IOException ex){
            
            ex.printStackTrace();
            Klient.log("Nie udało sie załadować mapy: wyjątek->" + ex.getMessage());
        } catch (ClassNotFoundException ex) {
           
            ex.printStackTrace();
            Klient.log("Nie udało sie załadować mapy: wyjątek->" + ex.getMessage());
        }
        this.myWQ.load();
    }
    public void load(LoadingFrame lf, Quarter nextQ, String map){
        
        if(nextQ != null) this.nextQ = nextQ;
        else this.nextQ = this;
        lf.updateSplash();
        if(map.equals("demo")) { loadDemoWQ(); }
        else { 
            myWQ= new WorldQ();
            this.myWQ.fromFile(map);}
        Ae = new ActionEngine(myWQ);
        lf.updateSplash();
        Vector3f lastCheck = myWQ.checkPoint.get(myWQ.checkPoint.size()-1);
        base = new Base("base1",lastCheck.x,lastCheck.y,lastCheck.z,1.0f);
        lf.updateSplash();
        aState = ActionState.NONE;
        if(player == (Bridge.isMulti?Klient.uS.IDinGame:0)){//wuuut?? 
             Ae.TowersToBuild.add(new Tower(1));
        }
        lf.updateSplash();
    }
    public int giveMob(Mob mob){
        Ae.addMob(mob);
        mob.setID(MP);
        return MP++;
    }
    public void giveMob(Mob mob, int ID){
        Ae.addMob(mob);
        mob.setID(ID);
    }
    public void killMob(int ID){
        Ae.explodeMob(ID);
    }
    private void beginRotate(){
        switch(part){
            case 0: 
                //rotate0
                break;
            case 1: 
            case 2: 
            case 3: 
                  Graphic.e.push();
                  Graphic.e.rotY(part*90);  
                break;
        }
    }
     private void endRotate(){
        switch(part){
            case 0: 
                //rotate0
                break;
            case 1: 
            case 2: 
            case 3: 
                  Graphic.e.pop(); 
                break;
        }
    }
    public void draw(){
        beginRotate();
        myWQ.draw(0);
        innerDraw();
        endRotate();     
    }
    private void input(){
        if(player == (Bridge.isMulti?Klient.uS.IDinGame:0)){
             if(Input.isKeyDown(Keyboard.KEY_ESCAPE)){
                Display.destroy();
                System.exit(1);
            }
            
            if(Input.LMBdown){
                if(aState == ActionState.BUILD){
                    aState = ActionState.NONE;
                    Ae.buildTower(aTower);
                 
                }else{
                    Pointer.refresh();
                }
            }
            else{
             if(Input.isKeyDown(Keyboard.KEY_1)){
               aState = ActionState.BUILD;
               aTower = 1;
              
                }

            }    
             if(Input.isKeyDown(Keyboard.KEY_DOWN)){
                 base.openGate();
             }
             else{
                    if(Input.isKeyDown(Keyboard.KEY_UP)){
                        base.closeGate();
               }
             }
            
        }
    }
    private void innerDraw(){  
        input();        
        //rysujemy atakowaną baze!
        base.draw();
        Ae.draw(aState, aTower,player);      
    }
}
